package com.h117121.fashionshop.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.h117121.fashionshop.constant.AppConstant;
import com.h117121.fashionshop.controller.generic.GenericController;
import com.h117121.fashionshop.core.service.AccountRestService;
import com.h117121.fashionshop.core.service.AccountRestService.CheckUsernameResult;

@Controller
@RequestMapping("/accountservice")
public class AccountServiceController extends GenericController {
	@Autowired
	private MessageSource messageSource;

	@Autowired
	private AccountRestService accountRestService;

	@RequestMapping(value = "/{username}", method = RequestMethod.GET)
	public @ResponseBody
	String checkUsername(@PathVariable String username,
			HttpServletRequest request, HttpServletResponse response,
			Locale locale) {
		CheckUsernameResult result = accountRestService.checkUsername(username);
		if (result == CheckUsernameResult.EXISTED) {
			return messageSource.getMessage("existed.userName", null, locale);
		} else if (result == CheckUsernameResult.INVALID) {
			return messageSource.getMessage("invalid.userName", null, locale);
		} else if (result == CheckUsernameResult.TOOSHORT) {
			return messageSource.getMessage("tooshort.userName",
					new Object[] { AppConstant.MINIMUM_CHARACTER_USERNAME },
					locale);
		} else {
			return messageSource.getMessage("valid.userName", null, locale);
		}
	}
}
